package cn.tedu.mall.seckill.consumer;

import cn.tedu.mall.pojo.seckill.model.Success;
import cn.tedu.mall.seckill.config.RabbitMqComponentConfiguration;
import cn.tedu.mall.seckill.mapper.SeckillSkuMapper;
import cn.tedu.mall.seckill.mapper.SuccessMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = {RabbitMqComponentConfiguration.SECKILL_QUEUE})
public class SeckillQueueConsumer {
    //业务需要的Mapper对象
    @Autowired
    private SuccessMapper successMapper;
    @Autowired
    private SeckillSkuMapper seckillSkuMapper;

    @RabbitHandler
    public void process(Success success){
        //先减少库存
        //真正减少数据库中的库存的时候，秒杀可能已经结束了
        //这个库存的减少操作也不会影响秒杀过程中redis的库存数
        seckillSkuMapper.updateReducesStockBySkuId(success.getSkuId(),success.getQuantity());
        //新增success对象到数据库
        successMapper.saveSuccess(success);
        //如果上面操作数据库发送异常
        //我们可以设计再向RabbitMQ发送消息来处理
        //把消息发送给死信队列，人工处理
    }
}
